<?php
/*
If accessing the sendy db directly, immediately convert to hashed sendy_id
*/
 
class Sendy {
	
	private $db;
	private $license_key;
	private $installation_url;
	private $subscriptions;
	private $user;
	
	public function __construct($user = null) {
	
		if(SENDY_LICENSE_KEY == "") die("Missing SENDY_LICENSE_KEY in app config");
		if(SENDY_API_KEY == "") die("Missing SENDY_API_KEY in app config");
		if(SENDY_DB == "") die("Missing SENDY_DB in app config");
		if(SENDY_DB == "") die("Missing SENDY_DB in app config");
		
		$this->license_key 		= SENDY_LICENSE_KEY;
		$this->api_key          = SENDY_API_KEY;
		$this->db 				= SENDY_DB;	
		$this->installation_url = SENDY_URL;	
							
		# Subscriptions
		if($user != null) {
			$this->user = $user;
			$q = "SELECT * FROM subscribers WHERE email = '".$this->user->email."' AND unsubscribed = 0";
			$subscriptions_raw = DB::instance($this->db)->select_array($q, 'list');
			$subscriptions     = Array();
			
			foreach($subscriptions_raw as $list_id => $data) {

				# Convert decimal to hash
				$list_id = self::sendy_encrypt($list_id);

				$subscriptions[$list_id] = $data;
				
			}		
						
			$this->subscriptions = $subscriptions;
		}
					
	}
	
		
	/*-------------------------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------------------------*/
	public function get_subscriptions() {
	
		return $this->subscriptions;
	
	}	
		
		
	/*-------------------------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------------------------*/
	public function get_lists() {
		
		$lists = DB::instance(SENDY_DB)->select_rows("SELECT * FROM lists");
		
		// Because we're tapping into the sendy db, we have to convert the list ids to hashes
		foreach($lists as $k => $v) {
			$lists[$k]['id'] = self::sendy_encrypt($v['id']);
		}
		
		return $lists;
		
	}
		
	/*-------------------------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------------------------*/	
	public function subscribe($list_id) {
	
		# Only subscribe if they're not already on the list, or they're on the list but unsubscribed
		//if(!isset($this->subscriptions[$list_id]) || $this->subscriptions[$list_id]['unsubscribed'] == 1) {
			$post_fields = "&boolean=true&name=".$this->user->first_name."%20".$this->user->last_name."&email=".$this->user->email."&list=".$list_id;			
			return Utils::curl($this->installation_url."/subscribe", 0, false, '', $post_fields, false);
		//}
			
		return 0;
	
	}
	
	
	/*-------------------------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------------------------*/
	public function unsubscribe($list_id) {
	
		# Only unsubscribe if they were subscribed
		//if(isset($this->subscriptions[$list_id]) && $this->subscriptions[$list_id]['unsubscribed'] == 0) {
			$post_fields = "&list=".$list_id."&boolean=true&email=".$this->user->email;
			return Utils::curl($this->installation_url."/unsubscribe", 0, false, '', $post_fields, false);
		//}
		
		return 0;
	}
	
	
	/*-------------------------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------------------------*/
	public function unsubscribe_by_email($email, $list_id) {
	
		$post_fields = "&list=".$list_id."&boolean=true&email=".$email;
		return Utils::curl($this->installation_url."/unsubscribe", 0, false, '', $post_fields, false);
	
	}
	
	
	/*-------------------------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------------------------*/
	public function get_subscriptions_by_email($email) {
	
		$subscriptions = DB::instance(SENDY_DB)->select_array("SELECT * FROM subscribers WHERE unsubscribed = 0 AND email = '".$email."'",'list');
			
		// Because we're tapping into the sendy db, we have to convert the list ids to hashes
		foreach($subscriptions as $k => $v) {
			$subscriptions[self::sendy_encrypt($k)] = $v;
		}
	
		return $subscriptions;
	
	}
	
	
	/*-------------------------------------------------------------------------------------------------
	2 way encrypt function
	Borrowed from /sendy/includes/short.php
	-------------------------------------------------------------------------------------------------*/
	public function sendy_encrypt($in, $to_num = false) {

		$encryptionMethod = "AES-256-CBC";
		
		if($to_num)
		{
			if(version_compare(PHP_VERSION, '5.3.0') >= 0) //openssl_decrypt requires at least 5.3.0
			{
				$decrypted = str_replace('888', '/', $in);
				$decrypted = str_replace('777', '+', $decrypted);
				
				if(function_exists('openssl_encrypt')) $decrypted = openssl_decrypt($decrypted, $encryptionMethod, SENDY_API_KEY, 0, '3j9hwG7uj8uvpRAT');
				else return intval($in, 36);
					
				if($decrypted=='') return intval($in, 36);
				else return $decrypted;
			}
			else
			{
				return intval($in, 36);
			}
		}
		else
		{	
			if(version_compare(PHP_VERSION, '5.3.0') >= 0) //openssl_encrypt requires at least 5.3.0
			{			
				if(function_exists('openssl_encrypt')) $encrypted = openssl_encrypt($in, $encryptionMethod, SENDY_API_KEY, 0, '3j9hwG7uj8uvpRAT');
				else return base_convert($in, 10, 36);
				
				$encrypted = str_replace('/', '888', $encrypted);
				$encrypted = str_replace('+', '777', $encrypted);
				$encrypted = str_replace('=', '', $encrypted);
				
				return $encrypted;
			}
			else
			{
				return base_convert($in, 10, 36);
			}
		}
	}
	
		
} # eoc